package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.page.PageList;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS/*有就添加事务 没有就不添加*/,readOnly = true/* 查询事务 */)//添加事务
public class BaseServiceImpl<T> implements IBaseService<T>{
    @Autowired
private BaseMapper<T> baseMapper;
    @Override
    public List queryAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T queryOne(Long id) {
        return baseMapper.loadOne(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList queryPage(BaseQuery baseQuery) {
        Integer total = baseMapper.loadTotal(baseQuery);//查询总条数
        List<T> list = baseMapper.loadData(baseQuery);//查询当前页数据
        return new PageList<T>(total,list);
    }

    @Override
    @Transactional
    public void bachDelete(List ids) {
        baseMapper.bachDelete(ids);
    }
}
